(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ResitemCtrl
     * @description
     * # ResitemCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ResitemCtrl', ResitemCtrl);

    ResitemCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function ResitemCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getlist(1);
            });
        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize
            };
            var sub = { store_id: $rootScope.store_id };
            if ($scope.res_item_id) {
                sub.res_item_id = $scope.res_item_id;
            }
            if ($scope.source_at) {
                sub.source_at = $scope.source_at;
            };
            query.query = sub;
            dataService.resitemGet(query).then(function(re1) {
                if (re1.res_item) {
                    $scope.res_item = re1.res_item;
                    $scope.total_count = re1.total_count;
                    if ($scope.res_item_id) {
                        $scope.resitem = $scope.res_item[0];
                        $scope.oldData = $scope.res_item[0];
                    }
                    console.error("schemas", $scope.resitem);
                }
            });
        }



        $scope.getperiod = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize
            };
            var sub = {};
            if ($scope.schema_id) {
                sub.schema_id = $scope.schema_id;
                // sub.status = 1;
            }
            if ($scope.start_source_at) {
                query.start_source_at = $scope.start_source_at;
                query.end_source_at = $scope.end_source_at;
            };

            query.query = sub;
            dataService.periodGet(query).then(function(re1) {
                if (re1.periods) {
                    $scope.periods = re1.periods;
                    $scope.total_count = re1.total_count;
                    console.error("periods", $scope.periods);
                }
            });
        }



        //添加数据 初始化
        $scope.addAction = function() {
            $scope.uid = random();
            $scope.res_item_id = $location.search().res_item_id;
            if ($scope.res_item_id) {
                $scope.getlist();
            } else {
                //初始化值
                // $scope.item.schema_type_id = 1;
                $scope.schema = [];
            }

        }

        //添加数据 初始化
        $scope.batchAction = function() {
            $scope.status = 1;
            $scope.start_source_at = $scope.data_time = moment().format('YYYY-MM-DD');
            $scope.end_source_at = moment().add('days', 7).format('YYYY-MM-DD');
        }

        //生成数据
        $scope.stltodat = function(id) {
            if (id) {
                var query = { "res_item_id": id };
                dataService.templatetodataAdd(query).then(function(re1) {

                });
            }

        }
        $scope.all = function(m) {
            for (var i = 0; i < $scope.periods.length; i++) {
                if (m === true) {
                    $scope.periods[i].status = true;
                } else {
                    $scope.periods[i].status = false;
                }
            }
        };

        $scope.saveDataperiods = function() {
            async.map($scope.periods, function(data, callback) {
                if (data) {
                    var iad = { "period_id": data.period_id, status: data.status }
                    dataService.periodUpdate(iad).then(function(r) {
                        callback(null);
                    })
                } else {
                    callback(null);
                }
            }, function(e, r) {
                helper.myAlert("success");
            });
        }

        $scope.savePeriods = function() {

            helper.myConfirm("确定删除此预约项目?", function() {

                var iad = { "schema_id": $scope.schema_id, start: $scope.start_source_at, end: $scope.end_source_at, status: $scope.status }

                dataService.periodStatusUpdate(iad).then(function(r) {
                    if (r.schema_id) {
                        helper.myAlert("success", null, null, function() {
                            $scope.getperiod();
                        });
                    } else {
                        helper.myAlert("error");
                    }
                })
            });


        }

        $scope.del = function(id) {
            helper.myConfirm("确定删除此预约项目?", function() {
                var data = { 'res_item_id': id };
                dataService.resitemDelete(data).then(function(result) {
                    if (result.res_item_id) {
                        helper.myAlert("success", null, null, function() {
                            $scope.getlist();
                        });
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }

        $scope.public = function(id) {
            var data = { 'res_item_id': id };
            dataService.periodsPublic(data).then(function(result) {
                if (result.res_item_id) {
                    helper.myAlert("success", null, null, function() {
                        $scope.getlist();
                    });
                } else {
                    helper.myAlert("error:" + result);
                }
            });
        }

        // schema_type_id
        $scope.saveData = function() {

            $scope.resitem.store_id = $rootScope.store_id;

            var zhuoziApiAry = {
                "add": "resitemAdd",
                "up": "resitemUpdate",
            }
            var api = "add";
            if ($scope.res_item_id) {
                api = "up";
            }

            dataService[zhuoziApiAry[api]]($scope.resitem).then(function(r) {
                if (r.res_item_id) {
                    helper.myAlert("success", null, null, function() {
                        if (!$scope.res_item_id) {
                            $scope.stltodat(r.res_item_id);
                        } else {
                            if ($scope.oldData.template_id == $scope.resitem.template_id) {
                                $scope.stltodat(r.res_item_id);
                            }
                        }
                        $state.go("resitem.list");
                    });
                } else {
                    helper.myAlert("error");
                }
            })
        }
    }
})();